$(function () {
    $.ajax({
        url: "./goSaleBack/initPage", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        dataType: "json", //返回格式为json
        type: "POST", //请求方式
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                // top.location.href = "../../login.html";
                window.location.href="../../login.html";
            }
            if(data.status=="1"){
                var data_json = JSON.parse(data.data);
                $("#StoreSelect").empty();
                if(data.store=="总部"){
                    $("#StoreSelect").append("<option selected=\"selected\" value=\"\">选择中心/店面</option>")
                    for(var i=0;i<data_json.data.length;i++){
                        $("#StoreSelect").append("<option value='"+data_json.data[i].Post+"'>"+data_json.data[i].Post+"</option>");
                    }
                }else{
                    $("#StoreSelect").append("<option selected=\"selected\" value=\""+data.store+"\">"+data.store+"</option>")
                    $("#StoreSelect").append("<option selected=\"selected\" value=\"\">选择中心/店面</option>")
                }

            }
        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });
})

//查询销售部岗位下拉
function getPostSelect() {
    var store = $("#StoreSelect").val();
    var jsondata = {
        "Store":store
    }
    var data_json = JSON.stringify(jsondata);
    $.ajax({
        url: "./goSaleBack/queryAllSalePostSelect", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        dataType: "json", //返回格式为json
        type: "POST", //请求方式
        data:data_json,
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                // top.location.href = "../../login.html";
                window.location.href="../../login.html";
            }
            if(data.code==200){
                var data_json = JSON.parse(data.data);
                $("#PostSelect").empty();
                $("#PostSelect").append("<option selected=\"selected\">选择岗位</option>")
                for(var i=0;i<data_json.data.length;i++){
                    $("#PostSelect").append("<option value='"+data_json.data[i].Post+"'>"+data_json.data[i].Post+"</option>");
                }

            }
        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });
}

//查询销售部岗位人员信息下拉
function getSaleManSelect() {
    var store = $("#StoreSelect").val();
    var post = $("#PostSelect").val();
    var jsondata = {
        "Store":store,
        "Post":post
    }
    var data_json = JSON.stringify(jsondata);
    $.ajax({
        url: "./goSaleBack/queryAllSaleManSelect", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        dataType: "json", //返回格式为json
        type: "POST", //请求方式
        data:data_json,
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                // top.location.href = "../../login.html";
                window.location.href="../../login.html";
            }
            if(data.code==200){
                var data_json = JSON.parse(data.data);
                $("#SaleManSelect").empty();
                $("#SaleManSelect").append("<option selected=\"selected\" value=\"\">选择姓名</option>")
                for(var i=0;i<data_json.data.length;i++){
                    $("#SaleManSelect").append("<option value='"+data_json.data[i].UserNo+"'>"+data_json.data[i].Name+"</option>");
                }

            }
        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });
}

//通过岗位、店面查询具有的类别(一级菜单)
function searchCategory() {
    var AssessDate = $("#time1").val();
    if(AssessDate==null||AssessDate==""||typeof(AssessDate) == "undefined"){
        toastr.warning('请录入考核时间并重新选择姓名');
        return false;
    }
    var store = $("#StoreSelect").val();
    var post = $("#PostSelect").val();
    var UserId = $("#SaleManSelect").val();
    if(UserId==null||UserId==''){
        return false;
    }
    var jsondata = {
        "Store":store,
        "Post":post
    }
    var data_json = JSON.stringify(jsondata);
    $.ajax({
        url: "./goSaleBack/queryAccessCategory", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        dataType: "json", //返回格式为json
        type: "POST", //请求方式
        data:data_json,
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                // top.location.href = "../../login.html";
                window.location.href="../../login.html";
            }
            if(data.code==200){
                var data_json = JSON.parse(data.data);
                var json = JSON.parse(data_json.data)
                $("#FirstFloor").empty();
                for(var i=0;i<json.length;i++){
                    var temStr = "first"+i;
                    $("#FirstFloor").append("<li class=\"onelevel\"><a onclick=\"return searchProject("+json[i].CID+","+i+")\">"+json[i].CName+"</a><ul id='"+temStr+"' class=\"twolevel_con\"></ul></li>"); //<span class="label label-success sum">160</span>
                }

            }
        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });
}

//通过类别查询类别具有的项目(二级菜单)
function searchProject(CID,index) {
    var store = $("#StoreSelect").val();
    var post = $("#PostSelect").val();
    var jsondata = {
        "CID":CID,
        "Store":store,
        "Post":post
    }
    var data_json = JSON.stringify(jsondata);
    $.ajax({
        url: "./goSaleBack/queryAccessProject", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        dataType: "json", //返回格式为json
        type: "POST", //请求方式
        data:data_json,
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                // top.location.href = "../../login.html";
                window.location.href="../../login.html";
            }
            if(data.code==200){
                var data_json = JSON.parse(data.data);
                var json = JSON.parse(data_json.data)
                var firstId = "#first"+index;
                var sss = $(firstId).attr("style");
                if(sss=="display:block"){
                    $(firstId).attr("style","display:none");
                    return false;
                }
                $(firstId).empty();
                for(var i=0;i<json.length;i++){
                    var temStr = "second"+json[i].PID;
                    $(firstId).append("<li class=\"twolevel\"><a onclick=\"return searchStandard("+json[i].PID+","+i+")\">"+json[i].PName+"</a><ul id='"+temStr+"' class=\"threelevel_con\"></ul></li>"); //<span class="label label-success sum">160</span>
                }
                $(firstId).attr("style","display:block")

            }
        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });
}

function searchStandard(PID,index) {
    var store = $("#StoreSelect").val();
    var post = $("#PostSelect").val();
    var jsondata = {
        "PID":PID,
        "Store":store,
        "Post":post
    }
    var data_json = JSON.stringify(jsondata);
    $.ajax({
        url: "./goSaleBack/queryAccessStandard", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        dataType: "json", //返回格式为json
        type: "POST", //请求方式
        data:data_json,
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                // top.location.href = "../../login.html";
                window.location.href="../../login.html";
            }
            if(data.code==200){
                var data_json = JSON.parse(data.data);
                var json = JSON.parse(data_json.data)
                var secondId = "#second"+PID;
                var sss = $(secondId).attr("style");
                if(sss=="display:block"){
                    $(secondId).attr("style","display:none");
                    return false;
                }
                $(secondId).empty();
                for(var i=0;i<json.length;i++){
                    var temStr = "third"+json[i].ID;
                    var temStr2 = "FinishRate"+json[i].ID;
                    var temStr3 = "Money"+json[i].ID;
                    var temStr4 = "IsOrNotMoney"+json[i].ID;
                    $(secondId).append("<li class=\"threeleve\" id =\""+temStr+"\"><a>"+json[i].Name+"</a><span><label>基数(元)：</label><input readonly=\"readonly\" type=\"text\" value=\""+json[i].Base+"\"></span>" +
                        "<span><label>占比(%)：</label><input readonly=\"readonly\" type=\"text\" value=\""+json[i].Proportion+"\"></span>" +
                        "<span><label>完成率(%)：</label><input id=\""+temStr2+"\" type=\"text\" onchange=\"changeMoney("+json[i].Base+","+json[i].Proportion+",this,"+temStr3+")\"></span>" +
                        "<span><label>是否领取当前岗位标准工资：</label><select id=\""+temStr4+"\"><option value=\"1\" selected='selected'>是</option><option value=\"0\">否</option></select></span>"+
                        "<label>合计：<span><input readonly=\"readonly\" id=\""+temStr3+"\"></span></label>"+"<input type='button' value='保存'  onclick='saveThisAccess("+temStr2+","+JSON.stringify(json[i])+","+temStr4+")'  /></li>");
                }
                $(secondId).attr("style","display:block")

            }
        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });


}

function changeMoney(Base,Proportion,obj,Money) {
    FinishRate = obj.value;
    var resultMoney = Base * (Proportion/100) * (FinishRate/100);
    resultMoney = resultMoney.toFixed(2);
    Money.value = resultMoney;
    return false;
}

//判断是否为0到1的小数
function nn(n){
    if(/^(0.\d+|0|1)$/.test(n)){
        return true;
    }else{
        return false;
    }
}
//两位小数点正数
function testZhengLiang(num) {
    if(!isNaN(num)){
        var dot = num.indexOf(".");
        if(dot != -1){
            var dotCnt = num.substring(dot+1,num.length);
            if(dotCnt.length > 2){
                return false;
            }
        }
    }else{
        return false;
    }
    if(num<0||num>100){
        return false;
    }
    return true;
}

function saveThisAccess(temStr2,obj,temStr4) {
    var FinRate = $('#'+temStr2.id).val();
    var IsOrNotMoney = $("#"+temStr4.id).val();

    if(FinRate==""||FinRate==null){
        toastr.warning('请录入完成率');
        return false;
    }
    var FinRateFlag = testZhengLiang(FinRate);
    if(FinRateFlag==false ){
        toastr.error('完成率必须为两位小数点以内小于等于100的正数');
        return false;
    }
    var AssessDate = $("#time1").val();
    var UserId = $("#SaleManSelect").val();
    if(AssessDate==""||AssessDate==null||UserId==""||UserId==null){
        toastr.warning('信息不完整，请查看是否选择日期与人员');
        return false;
    }
    obj.SalerID = UserId;
    obj.FinishRate = FinRate;
    obj.AssessDate = AssessDate;
    obj.IsOrNotMoney = IsOrNotMoney;
    var data_json = JSON.stringify(obj);
    $.ajax({
        url: "./goSaleBack/saveAssess", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        dataType: "json", //返回格式为json
        type: "POST", //请求方式
        data:data_json,
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                toastr.error('保存失败');
            }
            if(data.status==1){
                toastr.success('保存成功');
            }
            if(data.status==2){
                toastr.warning('本月已录入信息');
            }

        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });
}